import {Component, OnInit} from '@angular/core';
import {LoginService} from './login.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html'
})
export class LoginComponent implements OnInit {

  errorMsg: string;

  constructor(private router: Router, private loginService: LoginService) {
  }

  ngOnInit() {
  }

  onLogin(f: any) {
    this.loginService.onLogin(f.username, f.password)
      .then(data => {
        if (data.success) {
          localStorage.setItem('username', f.username);
          this.router.navigate(['home']);
        } else {
          this.errorMsg = data.errorMsg;
        }
      });
  }

}
